package com.ptrans.utilities;

import static com.ptrans.utilities.CommonUtilities.IMG_PATH;
import static com.ptrans.utilities.CommonUtilities.SERVER_URL;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.ImageView;

/**
 * Class with the CONSTANT and static method...
 * @author Jean
 *
 */
public final class CommonUtilities {

	public static final String SERVER_URL = "http://jeanhascoet.fr/pTrans/";
	public static final String IMG_PATH = "img/";
	public static final String SENDER_ID = "450193532989";
	public static final String TAG = "pTransMIP";
	public static final String DISPLAY_MESSAGE_ACTION = "com.ptrans.utilities.DISPLAY_MESSAGE";
	
	public final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
	
	public final static String PROPERTY_REG_ID = "registration_id";
	public final static String PROPERTY_USER_NAME = "user_name";
	public final static String PROPERTY_USER_MAIL = "user_mail";
	public final static String PROPERTY_USER_PICTURE = "user_picture";
	public final static String PROPERTY_APP_VERSION = "1.0";
	
	public static final String EXTRA_MESSAGE = "message";
	public static final String EXTRA_INFO = "info";
	public static final String EXTRA_SENDER = "sender";
	public static final String EXTRA_SENDER_NAME = "name";
	public static final String EXTRA_USER = "user";
	public static final String EXTRA_POSITION = "position";
	public static final String EXTRA_HEALTH = "health";
	public static final String EXTRA_CONNECT = "connect";
	
	public static final String PARAM_CONTROLLER = "c";
	public static final String PARAM_ACTION = "a";
	public static final String PARAM_USER_MAIL = "mail";
	public static final String PARAM_USER_NAME = "name";
	public static final String PARAM_REGID = "regId";
	public static final String PARAM_POSITION = "position";
	public static final String PARAM_HEALTH = "health";
	public static final String PARAM_MESSAGE = "message";
	public static final String PARAM_SENDER = "sender";
	
	public static final String RESPONSE_INFO = "info";
	public static final String RESPONSE_MESSAGE = "message";
	public static final String RESPONSE_STATE = "state";
	
	public static final String C_REGISTER = "UserController";
	public static final String C_MESSAGE = "MessageController";
	
	public static final String A_SENDALL = "sendAll";
	public static final String A_REGISTRATION = "registration";
	public static final String A_UNREGISTRATION = "unregistration";
	public static final String A_CONNECT = "connectGCM";
	public static final String A_DISCONNECT = "disconnectGCM";
	public static final String A_GET_USERS_ONLINE = "getConnectUsers";
	public static final String A_SET_POSITION = "setPosition";
	public static final String A_SET_HEALTH = "setHealth";
	public static final String A_VERIFY_NAME = "verifyName";
	
	public static void displayMessage(Context context, String message, String mode) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtra(mode, message);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    }
	
	public static String generateString(int n) {
		String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String pass = "";
		for(int i=0;i<n;i++) {
			int j = (int) Math.floor(Math.random() * chars.length());
			pass += chars.charAt(j);
		}
		return pass;
	}
	
	public static String encodeMessageUrl(String message) {
		return message.replace(" ", "+");
	}
	
	public static Bitmap getBitmapfromURL(String src){		//Récupérer une image à partir d'internet
		URL url = null;
		try {
			url = new URL(SERVER_URL + IMG_PATH + src);
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Bitmap bmp = null;
		try {
			bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
			return bmp;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void colorHealth(ImageView perso, int health){
		if(health==2){
			perso.setBackgroundColor(Color.GREEN);
		}else if(health==1){
			perso.setBackgroundColor(Color.YELLOW);
		}else if(health==0){
			perso.setBackgroundColor(Color.RED);
		}
		perso.setPadding(10, 10, 10, 10);
	}
	
	
}
